<template>
    <div class="app-container">
        <!-- 搜索模块 -->
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="项目名称" prop="valName">
                <el-input v-model="queryParams.valName" placeholder="项目名称" size="small" style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="项目状态" prop="valStatus">
                <el-select v-model="queryParams.valStatus" placeholder="项目状态" size="small" style="width: 240px">
                    <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                        :value="dict.dictValue" />
                </el-select>
            </el-form-item>
            <el-form-item label="项目进度" prop="valSchedule">
                <el-select v-model="queryParams.valSchedule" placeholder="项目进度" size="small" style="width: 240px">
                    <el-option v-for="dict in scheduleOptions" :key="dict.dictValue" :label="dict.dictLabel"
                        :value="dict.dictValue" />
                </el-select>
            </el-form-item>
            <el-form-item label="项目周期">
                <el-date-picker v-model="queryParams.scheduleRange" size="small" style="width: 240px"
                    value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-" start-placeholder="开始日期"
                    end-placeholder="结束日期" />
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                    type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 图标权限模块 -->
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button v-hasPermi="['admin:process:approval:add:post']" type="primary" plain icon="el-icon-plus"
                    size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPermi="['admin:process:approval:delete:{id}:delete']" type="danger" plain
                    icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">批量删除
                </el-button>
            </el-col>
            <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
        </el-row>
        <!-- 展示模块 -->
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="项目名称" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <router-link
                        :to="hasPermi(['']) ? '/process/approval/data/' + scope.row.id : '#'"
                        class="link-type">
                        <span>{{ scope.row.valName }}</span>
                    </router-link>
                </template>
            </el-table-column>
            <el-table-column label="立项金额" align="center" prop="valPrice" :show-overflow-tooltip="true" />
            <el-table-column label="开始时间" align="center" prop="valStartTime" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.valStartTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="valEndTime" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.valEndTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="项目状态" align="center" prop="valStatus" :formatter="statusFormat" />
            <el-table-column label="项目进度" align="center" prop="valSchedule" :formatter="scheduleFormat" />
            <el-table-column label="周期预警阈值" align="center" prop="valCycle" :show-overflow-tooltip="true" />
            <el-table-column label="投资预警阈值" align="center" prop="valInvest" :show-overflow-tooltip="true" />
            <el-table-column label="甲方负责人" align="center" prop="valPartyA" :show-overflow-tooltip="true" />
            <el-table-column label="甲方联系方式" align="center" prop="valPartyAPhone" :show-overflow-tooltip="true" />
            <el-table-column label="乙方负责人" align="center" prop="valPartyB" :show-overflow-tooltip="true" />
            <el-table-column label="乙方联系方式" align="center" prop="valPartyBPhone" :show-overflow-tooltip="true" />
            <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.create_datetime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="修改时间" align="center" prop="updateTime" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.update_datetime) }}</span>
                </template>
            </el-table-column>
            <el-table-column
                v-if="hasPermi(['admin:process:approval:updateshow:{id}:get', 'admin:process:approval:delete:{id}:delete'])"
                label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button v-hasPermi="['admin:process:approval:updateshow:{id}:get']" size="mini" type="text"
                        icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button v-hasPermi="['admin:process:approval:delete:{id}:delete']" size="mini" type="text"
                        icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />
        <!-- 新增 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="项目名称" prop="valName">
                    <el-input v-model="form.valName" placeholder="项目名称" :disabled="disabledValName" />
                </el-form-item>
                <el-form-item label="立项金额" prop="valPrice">
                    <el-input v-model="form.valPrice" placeholder="立项金额" />
                </el-form-item>
                <el-form-item label="项目周期" prop="cycle">
                    <el-date-picker v-model="form.cycle" style="width: 100%" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
                </el-form-item>
                <el-form-item label="项目状态" prop="valStatus">
                    <el-radio-group v-model="form.valStatus">
                        <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{
                            dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="项目进度" prop="valSchedule">
                    <el-radio-group v-model="form.valSchedule">
                        <el-radio v-for="dict in scheduleOptions" :key="dict.dictValue" :label="dict.dictValue">{{
                            dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="周期预警阈值" prop="valCycle">
                    <el-input v-model="form.valCycle" placeholder="百分比取值【选填,如果不填则按照字典全局设置】" />
                </el-form-item>
                <el-form-item label="投资预警阈值" prop="valInvest">
                    <el-input v-model="form.valInvest" placeholder="百分比取值【选填,如果不填则按照字典全局设置】" />
                </el-form-item>
                <el-form-item label="甲方负责人" prop="valPartyA">
                    <el-input v-model="form.valPartyA" placeholder="甲方负责人" />
                </el-form-item>
                <el-form-item label="联系方式" prop="valPartyAPhone">
                    <el-input v-model="form.valPartyAPhone" placeholder="甲方负责人联系方式" />
                </el-form-item>
                <el-form-item label="乙方负责人" prop="valPartyB">
                    <el-input v-model="form.valPartyB" placeholder="乙方负责人" />
                </el-form-item>
                <el-form-item label="联系方式" prop="valPartyBPhone">
                    <el-input v-model="form.valPartyBPhone" placeholder="乙方负责人联系方式" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listType, addType, updateShowType, uniqueType, delType } from "@/api/vadmin/process/approval/index";
import { Decrypt, Encrypt, HmacMD5 } from "@/utils/AesHmacMD5";

export default {
    name: "approval",
    created() {
        this.getList()
        this.getDicts("sys_normal_disable").then(response => {
            this.statusOptions = response.data;
        });
        this.getDicts("process_schedule").then(response => {
            this.scheduleOptions = response.data;
        });
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            valNames: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,  // 当前页数
                pageSize: 10,  // 每页显示几个
                valName: undefined,
                valStatus: undefined,
                valSchedule: undefined,
                scheduleRange: [],
            },
            // 状态数据字典
            statusOptions: [],
            // 项目进度数据字典
            scheduleOptions: [],
            // 创建时间日期范围
            dateRange: [],
            // 存储显示数据
            dataList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                valName: [
                    { required: true, validator: this.validatorValName(), trigger: "blur" }
                ],
                valPrice: [
                    { required: true, message: "立项金额不能为空", trigger: "blur" }
                ],
                cycle: [
                    { required: true, message: "项目周期不能为空", trigger: "blur" }
                ],
                valStatus: [
                    { required: true, message: "项目状态不能为空", trigger: "blur" }
                ],
                valSchedule: [
                    { required: true, message: "项目进度不能为空", trigger: "blur" }
                ]
            },
            disabledValName: false, // 新增可用 修改不可用
        }
    },
    methods: {
        // ============================================= 表单验证 ===================================
        validatorValName() {
            return (rule, value, callback) => {
                if (!value) {
                    return callback(new Error("项目名称不能为空"));
                } else if (this.form.id == undefined) {
                    uniqueType(value).then(response => {
                        if (response.data) callback();
                        else return callback(new Error("项目名称不能重复"));
                    })
                } else {
                    callback();
                }
            }
        },


        // ============================================= 表头条件查询 ===================================

        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                valName: undefined,
                valPrice: undefined,
                valStartTime: undefined,
                valEndTime: undefined,
                valStatus: this.selectDictDefault(this.statusOptions),
                valSchedule: this.selectDictDefault(this.scheduleOptions),
                valPartyA: undefined,
                valPartyAPhone: undefined,
                valPartyB: undefined,
                valPartyBPhone: undefined,
                cycle: undefined,
                valCycle: undefined,
                valInvest: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },

        // ============================================= 按钮操作 ===================================

        // 新增
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "项目新增";
            this.disabledValName = false;
        },

        // 修改
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            updateShowType(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "项目修改";
                this.disabledValName = true
            });
        },

        // 删除
        handleDelete(row) {
            const ids = row.id || this.ids;
            const names = row.valName || this.valNames
            this.$confirm('是否确认删除项目名称为"' + names + '"的数据项? 该项目之下的所有数据将会随之删除，请谨慎操作', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delType(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            });
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },

        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    addType(this.form).then(response => {
                        this.msgSuccess(response.msg);
                        this.open = false;
                        this.getList();
                    });
                }
            });
        },

        // ============================================= 数据展示Table表单 ===================================

        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.valNames = selection.map(item => item.valName);
            this.single = selection.length !== 1;
            this.multiple = selection.length < 2;
        },

        /** 查询模板文件列表 */
        getList() {
            this.loading = true;
            listType(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                var resData = JSON.parse(Decrypt(response.data))
                this.dataList = resData.results;
                this.total = resData.count;
                this.loading = false;
            });
        },

        // ============================================= 字典翻译 ===================================

        // 数据状态字典翻译
        statusFormat(row, column) {
            return this.selectDictLabel(this.statusOptions, row.valStatus);
        },
        // 项目进度翻译
        scheduleFormat(row, column) {
            return this.selectDictLabel(this.scheduleOptions, row.valSchedule);
        }

    }
}
</script>